package com.ssy.lingxi.pay.controller.feign;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.api.dto.request.aliPay.*;
import com.ssy.lingxi.pay.api.common.PayChannelEnum;
import com.ssy.lingxi.pay.model.assetAccount.AliPayResult;
import com.ssy.lingxi.pay.service.IAliPayService;
import jodd.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author lqa
 * @version 2.0.0
 * @menu 支付宝支付内部接口
 * 支付宝支付
 * @date 2021/11/2
 */
@RestController
@RequestMapping("/pay/aliPay")
public class AlipayFeignController extends BaseController {

    @Resource
    private IAliPayService aliPayService;

    @Resource
    private HttpServletRequest request;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 订单支付
     *
     * @param aliPayComputerRequest 接口参数
     * @return 支付页面
     */
    @RequestMapping(value = "/orderPay", method = RequestMethod.POST)
    public Wrapper<String> orderPay(@RequestBody @Valid AliPayComputerRequest aliPayComputerRequest) {
        Integer payChannel = aliPayComputerRequest.getPayChannel();
        AliPayResult payResult = null;
        if (payChannel == null || PayChannelEnum.Platform.getCode().equals(payChannel)) {
            payResult = aliPayService.pay(aliPayComputerRequest, request);
            if (payResult != null && StringUtil.isNotEmpty(payResult.getMessage())) {
                //返回支付路径
                return Wrapper.success(payResult.getMessage());
            }
        } else if (PayChannelEnum.APP.getCode().equals(payChannel)) {
            payResult = aliPayService.appPay(aliPayComputerRequest, request);
            if (payResult != null && StringUtil.isNotEmpty(payResult.getMessage())) {
                //返回支付路径
                return Wrapper.success(payResult.getMessage());
            }
        } else if (PayChannelEnum.JsApi.getCode().equals(payChannel)) {
            payResult = aliPayService.jsApiPay(aliPayComputerRequest, request);
            if (payResult != null && StringUtil.isNotEmpty(payResult.getMessage())) {
                //返回支付路径
                return Wrapper.success(payResult.getMessage());
            }
        }
        return Wrapper.fail(Objects.requireNonNull(payResult).getMessage());
    }

    /**
     * 查询订单
     *
     * @param aliPayOrderQueryRequest 接口参数
     * @return 操作结果
     */
    @RequestMapping(value = "/orderQuery", method = RequestMethod.POST)
    public Wrapper<Object> orderQuery(@RequestBody AliPayOrderQueryRequest aliPayOrderQueryRequest) {
        AliPayResult payResult = aliPayService.orderQuery(aliPayOrderQueryRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getMessage());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

    /**
     * 关闭订单
     *
     * @param aliPayCloseOrderRequest 接口参数
     * @return 操作结果
     */
    @RequestMapping(value = "/closeOrder", method = RequestMethod.POST)
    public Wrapper<Object> closeOrder(@RequestBody AliPayCloseOrderRequest aliPayCloseOrderRequest) {
        UserLoginCacheDTO sysUser = getSysUser();
        if (sysUser != null) {
            aliPayCloseOrderRequest.setOperateId(sysUser.getMemberId());
        }
        AliPayResult payResult = aliPayService.closeOrder(aliPayCloseOrderRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getMessage());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }

    /**
     * 申请退款
     *
     * @param aliPayRefundRequest 接口参数
     * @return 操作结果
     */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public Wrapper<Object> refund(@RequestBody AliPayRefundRequest aliPayRefundRequest) {
        log.info("支付宝申请退款参数-->:{}", aliPayRefundRequest);
        AliPayResult payResult = aliPayService.refund(aliPayRefundRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getMessage());
        } else {
            return Wrapper.fail(Integer.parseInt(payResult.getCode()), payResult.getMessage());
        }
    }

    /**
     * 查询退款
     *
     * @param aliPayRefundQueryRequest 接口参数
     * @return 操作结果
     */
    @RequestMapping(value = "/refundQuery", method = RequestMethod.POST)
    public Wrapper<Object> refundQuery(@RequestBody AliPayRefundQueryRequest aliPayRefundQueryRequest) {
        AliPayResult payResult = aliPayService.refundQuery(aliPayRefundQueryRequest);
        if (payResult.isSuccess()) {
            return Wrapper.success(payResult.getMessage());
        } else {
            return Wrapper.fail(payResult.getMessage());
        }
    }


}
